package com.zrq.gencode.cfg;

import com.zrq.gencode.exception.ConfigationException;
import com.zrq.gencode.properties.Propertiez;
import com.zrq.gencode.util.Comvlt;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 代码生成配置类
 *
 * @author ZRQ
 */
public class GenCfg {
	private static final Logger logger = LoggerFactory.getLogger(GenCfg.class);

	/**
	 * 总包名
	 */
	private static final String PACKAGE = "gencode.cfg.package";

	/**
	 * 要去除的前缀
	 */
	private static final String TABLE_PREFIX = "gencode.cfg.tablePrefix";

	/**
	 * 实体包名
	 */
	private static final String ENTITY = "gencode.cfg.package.entity";

	/**
	 * 业务接口层包名
	 */
	private static final String SERVICE = "gencode.cfg.package.service";
	/**
	 * 业务接口命名规范
	 */
	private static final String SERVICE_NAME = "gencode.cfg.package.serviceName";
	/**
	 * 业务接口实现层包名
	 */
	private static final String SERVICE_IMPL = "gencode.cfg.package.serviceImpl";
	/**
	 * 业务接口实现层命名规范
	 */
	private static final String SERVICE_IMPL_NAME = "gencode.cfg.package.serviceImplName";

	/**
	 * 控制层包名
	 */

	private static final String CONTROLLER = "gencode.cfg.package.controller";
	/**
	 * 控制层命名规范
	 */
	private static final String CONTROLLER_NAME = "gencode.cfg.package.controllerName";
	/**
	 * 持久层包名
	 */
	private static final String DAO = "gencode.cfg.package.dao";
	/**
	 * 持久层命名规范
	 */
	private static final String DAO_NAME = "gencode.cfg.package.daoName";

	/**
	 * 是否启用多线程模式
	 */
	private static final String ENABLE_MULTITHREADING = "gencode.cfg.enableMultithreading";

	/**
	 * 线程池等待超时时间（分钟）
	 */
	private static final String THREAD_POOL_TIMEOUT_MINUTES = "gencode.cfg.threadPoolTimeoutMinutes";

	/**
	 * 作者
	 */
	private static final String AUTHOR = "gencode.cfg.author";
	/**
	 * 作者
	 */
	public static String author = Propertiez.properties.getProperty(AUTHOR);
	/**
	 * 实体包名
	 */
	public static String entityPackageName = "entity";
	/**
	 * 业务接口层包名
	 */
	public static String servicePackageName = "service";
	/**
	 * 控制层包名
	 */
	public static String webPackageName = "controller";
	/**
	 * 持久层包名
	 */
	public static String daoPackageName = "dao";
	/**
	 * 业务实现层包名
	 */
	public static String serviceablePackageName = "impl";
	public static String packagePath = "";
	public static boolean isAutoRemovePre;
	/**
	 * 业务接口命名规范
	 */
	public static String serviceName = "EntityService";
	/**
	 * 业务接口实现层命名规范
	 */
	public static String serviceImplName = "EntityServiceImpl";
	/**
	 * 控制层命名规范
	 */
	public static String controllerName = "EntityController";
	/**
	 * 持久层命名规范
	 */
	public static String daoName = "EntityDao";

	/**
	 * 是否启用多线程模式
	 */
	public static boolean enableMultithreading = false;

	/**
	 * 线程池等待超时时间（分钟）
	 */
	public static int threadPoolTimeoutMinutes = 10;

	static {
		entityPackageName = Comvlt.defaultString(Propertiez.properties.getProperty(ENTITY), entityPackageName);
		servicePackageName = Comvlt.defaultString(Propertiez.properties.getProperty(SERVICE), servicePackageName);
		webPackageName = Comvlt.defaultString(Propertiez.properties.getProperty(CONTROLLER), webPackageName);
		daoPackageName = Comvlt.defaultString(Propertiez.properties.getProperty(DAO), daoPackageName);
		serviceablePackageName = Comvlt.defaultString(Propertiez.properties.getProperty(SERVICE_IMPL),
		                                              serviceablePackageName);
		author = Comvlt.defaultString(System.getProperty("user.name"), author);
		String packagePath = Propertiez.properties.getProperty(PACKAGE);
		if (StringUtils.isBlank(packagePath)) {
			throw new ConfigationException("总包名未配置");
		}
		GenCfg.packagePath = Comvlt.packageSupplement(packagePath);
		logger.info("设置包路径: {}", GenCfg.packagePath);
		isAutoRemovePre = StringUtils.isNotBlank(getTablePrefix());
		if (isAutoRemovePre) {
			logger.info("启用表前缀自动移除功能，前缀: {}", getTablePrefix());
		}
		serviceName = Comvlt.defaultString(Propertiez.properties.getProperty(SERVICE_NAME), serviceName);
		serviceImplName = Comvlt.defaultString(Propertiez.properties.getProperty(SERVICE_IMPL_NAME), serviceImplName);
		controllerName = Comvlt.defaultString(Propertiez.properties.getProperty(CONTROLLER_NAME), controllerName);
		daoName = Comvlt.defaultString(Propertiez.properties.getProperty(DAO_NAME), daoName);
		// 读取多线程配置，默认为false
		String enableMultithreadingStr = Propertiez.properties.getProperty(ENABLE_MULTITHREADING);
		if (StringUtils.isNotBlank(enableMultithreadingStr)) {
			enableMultithreading = Boolean.parseBoolean(enableMultithreadingStr);
		}
		logger.info("多线程模式: {}", enableMultithreading ? "启用" : "禁用");
		if (enableMultithreading) {
			// 读取线程池超时时间配置，默认为10分钟
			String threadPoolTimeoutMinutesStr = Propertiez.properties.getProperty(THREAD_POOL_TIMEOUT_MINUTES);
			if (StringUtils.isNotBlank(threadPoolTimeoutMinutesStr)) {
				try {
					threadPoolTimeoutMinutes = Integer.parseInt(threadPoolTimeoutMinutesStr);
				} catch (NumberFormatException e) {
					logger.warn("线程池超时时间配置无效，使用默认值10分钟: {}", threadPoolTimeoutMinutesStr);
				}
			}
			logger.info("线程池等待超时时间: {} 分钟", threadPoolTimeoutMinutes);
		}
	}

	/**
	 * 实体全路径名 例：com.zrq.gencode.entity
	 *
	 */
	public static String getCompleteEntity() {
		return (Propertiez.properties.getProperty(PACKAGE) + Comvlt.POIT + entityPackageName).toLowerCase()
			.replace(Comvlt.SLASH, Comvlt.POIT);
	}

	/**
	 * 业务接口全路径名 例：com.zrq.gencode.service
	 *
	 */
	public static String getCompleteService() {
		return (Propertiez.properties.getProperty(PACKAGE) + Comvlt.POIT + servicePackageName).toLowerCase()
			.replace(Comvlt.SLASH, Comvlt.POIT);
	}

	/**
	 * 业务实现全路径名 例：com.zrq.gencode.service.impl
	 *
	 */
	public static String getCompleteServiceImpl() {
		return getCompleteService() + Comvlt.POIT + serviceablePackageName;
	}

	/**
	 * 控制器全路径名 例：com.zrq.gencode.web
	 *
	 */
	public static String getCompleteWeb() {
		return (Propertiez.properties.getProperty(PACKAGE) + Comvlt.POIT + webPackageName).toLowerCase()
			.replace(Comvlt.SLASH, Comvlt.POIT);
	}

	/**
	 * 持久层全路径名 例：com.zrq.gencode.web
	 *
	 */
	public static String getCompleteDao() {
		return (Propertiez.properties.getProperty(PACKAGE) + Comvlt.POIT + daoPackageName).toLowerCase()
			.replace(Comvlt.SLASH, Comvlt.POIT);
	}

	/**
	 * 表名前缀
	 *
	 */
	public static String getTablePrefix() {
		return Propertiez.properties.getProperty(TABLE_PREFIX);
	}

	/**
	 * 实体包全限定名
	 *
	 */
	public static String getEntity() {
		return Comvlt.prefixSupplement(packagePath, Comvlt.prefixSupplement(Comvlt.SLASH, entityPackageName))
			.replace(Comvlt.POIT, Comvlt.SLASH);
	}

	/**
	 * 业务接口包全限定名
	 *
	 */
	public static String getService() {
		return Comvlt.prefixSupplement(packagePath, Comvlt.prefixSupplement(Comvlt.SLASH, servicePackageName))
			.replace(Comvlt.POIT, Comvlt.SLASH);
	}

	/**
	 * 控制层包全限定名
	 *
	 */
	public static String getWeb() {
		return Comvlt.prefixSupplement(packagePath, Comvlt.prefixSupplement(Comvlt.SLASH, webPackageName))
			.replace(Comvlt.POIT, Comvlt.SLASH);
	}

	/**
	 * 持久层包全限定名
	 *
	 */
	public static String getDao() {
		return Comvlt.prefixSupplement(packagePath, Comvlt.prefixSupplement(Comvlt.SLASH, daoPackageName))
			.replace(Comvlt.POIT, Comvlt.SLASH);
	}

	/**
	 * 业务实现包全限定名
	 *
	 */
	public static String getServiceimpl() {
		return (getService() + Comvlt.SLASH + serviceablePackageName).replace(Comvlt.POIT, Comvlt.SLASH);
	}

}